import { defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
// import AutoImport from 'unplugin-auto-import/vite'
// import Components from 'unplugin-vue-components/vite'
// import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'
// import commonjs from 'rollup-plugin-commonjs';
// import externalGlobals from 'rollup-plugin-external-globals';
import { visualizer } from 'rollup-plugin-visualizer';
// 配置svg图标
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

// 引入Unocss
import Unocss from 'unocss/vite';
import { presetUno, presetAttributify, presetIcons } from 'unocss'


export default defineConfig({
	define: { 'process.env': {} },
	resolve: {
		alias: {
			'@': path.resolve(__dirname, 'src'),
			'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
		}
	},
	base: './',
	server: {
		host: '0.0.0.0',
		port: '5200',
		open: true,

	},
	plugins: [
		vue(),
		Unocss({ // 使用Unocss
			presets: [
				presetUno(),
				presetAttributify(),
				presetIcons()
			],
		}),
		visualizer({
			emitFile: false,
			file: "stats.html", //分析图生成的文件名
			open: true //如果存在本地服务端口，将在打包后自动展示
		}),
		createSvgIconsPlugin({
			// 指定需要缓存的图标文件夹
			iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
			// 指定symbolId格式
			symbolId: 'icon-[dir]-[name]',
		}),
		// AutoImport({
		// 	resolvers: [ElementPlusResolver()]
		// }),
		// Components({
		// 	resolvers: [ElementPlusResolver()]
		// }),
		viteCompression({
			verbose: true,
			disable: false,
			threshold: 10240,
			algorithm: 'gzip',
			ext: '.gz'
		})
	],
	build: {
		minify: 'terser',
		terserOptions: {
			format: {
				comments: false
			}
		},
		chunkSizeWarningLimit: 1500,
		rollupOptions: {
			// external: ['vue', 'element-plus', 'vue-demi'],
			// plugins: [
			// 	commonjs(),
			// 	externalGlobals({
			// 		vue: 'Vue',
			// 		'element-plus': 'ElementPlus',
			// 		'vue-demi': 'VueDemi',
			// 	})
			// ],
			output: {
				chunkFileNames: 'static/js/[name]-[hash].js',
				entryFileNames: 'static/js/[name]-[hash].js',
				assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
				manualChunks (id) {
					if (id.includes('node_modules')) {
						return id.toString().split('node_modules/')[1].split('/')[0].toString()
					}
				}
			}
		}
	},
	css: {
		preprocessorOptions: {
			scss: {
				// additionalData: '@import "src/styles/common.scss";',
			}
		}
	}
})
